package DoublePointer;

/**
 * @BelongsProject: SeniorArchitect-LeetCode
 * @BelongsPackage: DoublePointer
 * @Author: zhuangxiaoyan
 * @CreateTime: 2023-10-18  22:06
 * @Description: TODO
 * @Version: 1.0
 */
public class 验证回文串II680 {

    // 判断字符串i-j的位置是否市一个回文串
    public boolean isPalindromeRange(String s, int i, int j) {
        for (int k = i; k < (i + (j - i) / 2); k++) {
            if (s.charAt(k) != s.charAt(j - k + i)) {
                return false;
            }
        }
        return true;
    }

    //  时间为o(n) 空间为O(1) 之遍历了一边的结果出来了
    public boolean validPalindrome(String s) {
        int i = 0;
        int j = s.length() - 1;
        while (i < j) {
            if (s.charAt(i) != s.charAt(j)) {
                return isPalindromeRange(s, i + 1, j) || isPalindromeRange(s, i, j - 1);
            }
            i++;
            j--;
        }
        return true;
    }
}
